<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>商品列表</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="/js/elementui/index.css">
</head>
<body>
<div id="app">
    <el-button type="primary" @click="dialogFormVisible = true">添加产品</el-button>
    <!--数据表格-->
    <el-table
            :data="tableData"
            style="width: 100%">
        <el-table-column label="编号">
            <template slot-scope="scope">
                <span >{{ scope.row.id }}</span>
            </template>
        </el-table-column>
        <el-table-column label="名称">
            <template slot-scope="scope">
                <span >{{ scope.row.productName }}</span>
            </template>
        </el-table-column>
        <el-table-column label="价格">
            <template slot-scope="scope">
                <span >{{ scope.row.price }}</span>
            </template>
        </el-table-column>
        <el-table-column label="类型">
            <template slot-scope="scope">
                <span >{{ scope.row.productType }}</span>
            </template>
        </el-table-column>
        <el-table-column label="描述">
            <template slot-scope="scope">
                <span >{{ scope.row.productDesc }}</span>
            </template>
        </el-table-column>
        <el-table-column label="创建日期">
            <template slot-scope="scope">
                <span >{{ scope.row.createTime }}</span>
            </template>
        </el-table-column>
        <el-table-column label="品牌">
            <template slot-scope="scope">
                <span >{{ scope.row.productBrand }}</span>
            </template>
        </el-table-column>
        <el-table-column label="图片">
            <template slot-scope="scope">
                <span >{{ scope.row.productImage }}</span>
            </template>
        </el-table-column>
        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
            background
            layout="prev, pager, next"
            @current-change="loadPage"
            :total="total"
            :current-page="current"
            :page-size="pageSize">
    </el-pagination>
    <!--保存用户的对话框-->
    <el-dialog title="产品信息" :visible.sync="dialogFormVisible" @close="closeMyDialog">
        <el-form :model="product">
            <el-form-item label="产品名称" :label-width="formLabelWidth">
                <el-input v-model="product.productName" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="产品图片" :label-width="formLabelWidth">
                <el-input v-model="product.productImage" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="产品价格" :label-width="formLabelWidth">
                <el-input v-model="product.price" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="产品类型" :label-width="formLabelWidth">
                <el-input v-model="product.productType" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="产品描述" :label-width="formLabelWidth">
                <el-input v-model="product.productDesc" autocomplete="off"></el-input>
            </el-form-item>
<!--            <el-form-item label="创建时间" :label-width="formLabelWidth">-->
<!--                <el-input v-model="product.createTime" autocomplete="off"></el-input>-->
<!--            </el-form-item>-->
            <el-form-item label="产品品牌" :label-width="formLabelWidth">
                <el-input v-model="product.productBrand" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveProduct">确 定</el-button>
        </div>
    </el-dialog>
</div>
<script src="/js/vue/vue.js"></script>
<script src="/js/axios/axios.min.js"></script>
<!-- 引入组件库 -->
<script src="/js/elementui/index.js"></script>
<script>
    new Vue({
        el:"#app",
        data() {
            return {
                tableData: null,//表数据
                total:0,//总条数
                current:1,//当前条数
                pageSize:10, //每页条数
                dialogFormVisible: false,//控制对话框的显示
                product: {      //产品对话框的数据
                    id: '',
                    productName: '',
                    productImage: '',
                    price: '',
                    productType: '',
                    productDesc: '',
                    createTime: '',
                    productBrand: ''
                },
                formLabelWidth: '120px' //表单标签的宽度
            }
        },
        methods: {
            //显示修改用户的对话框
            handleEdit(index, row) {
                console.log(index, row);
                this.product = row;
                this.dialogFormVisible = true;
            },
            handleDelete(index, row) {
                console.log(index, row);
            },
            //当前页跳转的方法
            loadPage(current){
                this.current = current;
                //查询商品
                axios.get("/products?current="+current).then(value=>{
                    if(value.data.code == 1){
                        this.tableData = value.data.data.records;
                        this.total = value.data.data.total;
                        this.pageSize = value.data.data.size;
                        this.current = value.data.data.current;
                    }
                });
            },
            //关闭对话框
            closeMyDialog(){
                //清空数据
                this.product = {      //产品对话框的数据
                    id: '',
                    productName: '',
                    productImage: '',
                    price: '',
                    productType: '',
                    productDesc: '',
                    createTime: '',
                    productBrand: ''
                }
            },
            //保存用户的方法
            saveProduct(){
                //判断有id进行更新，否则添加
                if(this.product.id == ''){
                    axios.post("/product",this.product)
                        .then(value => {
                        if(value.data.code == 1){
                            this.loadPage(this.current);
                        }
                    });
                }else{
                    axios.put("/product",this.product)
                        .then(value => {
                        if(value.data.code == 1){
                            this.loadPage(this.current);
                        }
                    });
                }
                this.dialogFormVisible = false;
            }
        },
        mounted(){
            this.loadPage(1);
        }
    });
</script>
</body>
</html>